<template>
  <div class="menu">
    <el-card
      class="box-card"
      shadow="never"
      :body-style="{ height: '0px', padding: '0px 0px' }"
    >
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'menu-create' })" v-permission="15"
          >添加菜单</el-button
        >
      </div>
    </el-card>
    <el-table :data="menus" style="width: 100%" border>
      <el-table-column label="编号" width="100" type="index" align="center">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" min-width="150" align="center">
      </el-table-column>
      <el-table-column prop="level" label="菜单级数" min-width="150" align="center">
      </el-table-column>
      <el-table-column prop="icon" label="前端图标" min-width="150" align="center">
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" min-width="150" align="center">
      </el-table-column>
      <el-table-column label="操作" min-width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEdit(scope.row)" v-permission="15">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)" v-permission="18"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllMenus, deleteMenu } from '@/services/menu'

export default Vue.extend({
  name: 'MenuIndex',
  data () {
    return {
      menus: []
    }
  },

  created () {
    this.loadAllMenus()
  },

  methods: {
    async loadAllMenus () {
      const { data } = await getAllMenus()
      if (data.code === '000000') {
        this.menus = data.data
      }
    },

    handleEdit (item: FromServer.MenuItem) {
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: item.id.toString()
        }
      })
    },

    handleDelete (item: FromServer.MenuItem) {
      this.$confirm('确认删除吗？', '删除提示', {})
        .then(async () => {
          const { data } = await deleteMenu(item.id)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.loadAllMenus() // 更新数据列表
          }
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    }
  }
})
</script>

<style lang="scss" scoped></style>
